package intro;

/**
 * 创建不同状态的线程 并使用 jstack 观察
 * 
 * @author insight
 * @since 2021/8/23
 */
public class ThreadState {
    public static void main(String[] args) {
        // java.lang.Thread.State: TIMED_WAITING (sleeping)
        new Thread(new TimeWaiting(), "TimeWaiting").start();
        // java.lang.Thread.State: WAITING (on object monitor)
        new Thread(new Waiting(), "Waiting").start();
        // java.lang.Thread.State: RUNNABLE
        new Thread(new Blocked(), "Blocked-1").start();
        // java.lang.Thread.State: BLOCKED (on object monitor)
        new Thread(new Blocked(), "Blocked-2").start();
    }

    /**
     * 不断进行计时睡眠
     */
    static class TimeWaiting implements Runnable {
        @Override
        public void run() {
            while (true) {
                ThreadUtil.second(10);
            }
        }
    }

    /**
     * 在类上等待
     */
    static class Waiting implements Runnable {
        @Override
        public void run() {
            synchronized (Waiting.class) {
                try {
                    Waiting.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取到锁后不释放 其他线程阻塞
     */
    static class Blocked implements Runnable {
        @Override
        public void run() {
            synchronized (Blocked.class) {
                while (true) {
                    
                }
            }
        }
    }
}